from building import *

CWD = GetCurrentDir()

objs = Glob('src/*.c')


@register_library
class LibraryCrc(Library):
    def gen(self):
        pkg = Package(url='https://pycrc.org/download/pycrc-0.10.0.zip')
        pkg = os.path.join(pkg, 'pycrc-0.10.0/src')
        if (not os.path.exists('%s/src/crc8/crc8.c' % (CWD))):
            # @SWS_Crc_00031
            # ref https://github.com/IMProject/IMUtility/blob/main/Src/crc/crc8_variants/crc8_sae_j1850_zero.c
            MKDir('%s/src/crc8' % (CWD))
            cmd = '{0} {1}/pycrc.py --model crc-8 --poly 0x1D --xor-in 0xFF --xor-out 0xFF --reflect-in False --reflect-out False --algorithm table-driven --generate h -o {2}/src/crc8/crc8.h'.format(
                sys.executable, pkg, CWD)
            RunCommand(cmd)
            cmd = '{0} {1}/pycrc.py --model crc-8 --poly 0x1D --xor-in 0xFF --xor-out 0xFF --reflect-in False --reflect-out False --algorithm table-driven --generate c -o {2}/src/crc8/crc8.c'.format(
                sys.executable, pkg, CWD)
            RunCommand(cmd)
        if (not os.path.exists('%s/src/crc8h2f/crc8h2f.c' % (CWD))):
            # @SWS_Crc_00043
            # ref https://github.com/IMProject/IMUtility/blob/main/Src/crc/crc8_variants/crc8_8h2f.c
            MKDir('%s/src/crc8h2f' % (CWD))
            cmd = '{0} {1}/pycrc.py --model crc-8 --poly 0x2F --xor-in 0xFF --xor-out 0xFF --reflect-in False --reflect-out False --algorithm table-driven --generate h -o {2}/src/crc8h2f/crc8h2f.h'.format(
                sys.executable, pkg, CWD)
            RunCommand(cmd)
            cmd = '{0} {1}/pycrc.py --model crc-8 --poly 0x2F --xor-in 0xFF --xor-out 0xFF --reflect-in False --reflect-out False --algorithm table-driven --generate c -o {2}/src/crc8h2f/crc8h2f.c'.format(
                sys.executable, pkg, CWD)
            RunCommand(cmd)
        if (not os.path.exists('%s/src/crc16/crc16.c' % (CWD))):
            # @SWS_Crc_00019
            # ref https://github.com/IMProject/IMUtility/blob/main/Src/crc/crc16_variants/crc16_ccit_false.c
            MKDir('%s/src/crc16' % (CWD))
            cmd = '{0} {1}/pycrc.py --model crc-16 --poly 0x1021 --xor-in 0xFFFF --xor-out 0x0000 --reflect-in False --reflect-out False --algorithm table-driven --generate h -o {2}/src/crc16/crc16.h'.format(
                sys.executable, pkg, CWD)
            RunCommand(cmd)
            cmd = '{0} {1}/pycrc.py --model crc-16 --poly 0x1021 --xor-in 0xFFFF --xor-out 0x0000 --reflect-in False --reflect-out False --algorithm table-driven --generate c -o {2}/src/crc16/crc16.c'.format(
                sys.executable, pkg, CWD)
            RunCommand(cmd)
        if (not os.path.exists('%s/src/crc32/crc32.c' % (CWD))):
            # @SWS_Crc_00020
            # ref https://github.com/IMProject/IMUtility/blob/main/Src/crc/crc32_variants/crc32.c
            MKDir('%s/src/crc32' % (CWD))
            cmd = '{0} {1}/pycrc.py --model crc-32 --poly 0x04C11DB7 --reflect-in True --reflect-out True --algorithm table-driven --generate h -o {2}/src/crc32/crc32.h'.format(
                sys.executable, pkg, CWD)
            RunCommand(cmd)
            cmd = '{0} {1}/pycrc.py --model crc-32 --poly 0x04C11DB7 --reflect-in True --reflect-out True --algorithm table-driven --generate c -o {2}/src/crc32/crc32.c'.format(
                sys.executable, pkg, CWD)
            RunCommand(cmd)
        if (not os.path.exists('%s/src/crc32p4/crc32p4.c' % (CWD))):
            # @SWS_Crc_00058
            MKDir('%s/src/crc32p4' % (CWD))
            cmd = '{0} {1}/pycrc.py --model crc-32 --poly 0xF4ACFB13 --reflect-in True --reflect-out True --algorithm table-driven --generate h -o {2}/src/crc32p4/crc32p4.h'.format(
                sys.executable, pkg, CWD)
            RunCommand(cmd)
            cmd = '{0} {1}/pycrc.py --model crc-32 --poly 0xF4ACFB13 --reflect-in True --reflect-out True --algorithm table-driven --generate c -o {2}/src/crc32p4/crc32p4.c'.format(
                sys.executable, pkg, CWD)
            RunCommand(cmd)

    def config(self):
        self.gen()
        self.include = ['%s/src' % (CWD), '%s/src/crc8' % (CWD), '%s/src/crc8h2f' % (CWD),
                        '%s/src/crc16' % (CWD), '%s/src/crc32' % (CWD)]
        self.CPPPATH = ['$INFRAS']
        self.source = objs
